#include "usbupdate.h"
#include <QFile>
#include <QDir>
#include "ycompress.h"
#include "../DbSetting.h"
#include "mylogger.h"
#include "machineutil.h"

using namespace ns_yht;

UsbUpdate::UsbUpdate(const QString &appDir)
{
    m_appDir = appDir;
}

UsbUpdate::~UsbUpdate()
{
}

bool UsbUpdate::scanUpdate()
{
#if defined(Q_OS_LINUX)
    QDir dir("/udisk");
    if (!dir.exists())
        return false;

    // 如果U盘上有blackboxbackup文件夹，则备份日志文件和配置文件到该目录
    QDir logdir = QDir("/udisk/blackboxBackup");
    if (logdir.exists())
    {
        QString destDir = "/udisk/blackboxBackup/" + MachineUtil::machineCode();
        logdir.mkdir(MachineUtil::machineCode());
        logdir.setPath(destDir);

        // 备份原配置文件
        const QString src = m_appDir + "/setting.ini";
        const QString dest = QString("%1/setting%2.ini").arg(destDir, QDateTime::currentDateTime().toString("yyyyMMddHHmmss"));
        bool ret = QFile::copy(src, dest);
        if (GLOG)
            GLOG->info("backup setting.ini to udisk {} {} {}", src.toStdString(), dest.toStdString(), ret);

        QString dir2 = m_appDir + "/logs/";
        QDir logdir2 = QDir(dir2);
        logdir.mkdir("logs");
        destDir = QString("%1/logs").arg(destDir);

        foreach (QString file, logdir2.entryList())
        {
            QFile::copy(dir2 + file, destDir + "/" + file);
        }
        if (GLOG)
            GLOG->info("copy log files to /udisk/blackboxBackup/[deviceSerial]/logs");
    }
    DbSetting setting;
    QStringList filter;
    // 配置文件setting.ini更新
    filter << "setting????????.ini";
    QStringList files = dir.entryList(filter, QDir::Files | QDir::Readable, QDir::Name);
    if (files.size() > 0)
    {
        const QString &iniFile = files.last();
        QString iniVersion = setting.getSetting(SETTING_INIVERSION);
        if (iniFile > iniVersion)
        {
            bool ret = QFile::remove(m_appDir + "/setting.ini");
            if (!ret)
            {
                if (GLOG)
                    GLOG->error("remove old setting.ini failed  {}", QString(m_appDir + "/setting.ini").toStdString());
            }
            ret = QFile::copy("/udisk/" + iniFile, m_appDir + "/setting.ini");
            if (ret)
            {
                setting.saveSetting(SETTING_INIVERSION, files.last());
                if (GLOG)
                    GLOG->info("update setting.ini");
            }
            else if (GLOG)
                GLOG->error("copy setting.ini failed");
        }
        else if (GLOG)
        {
            GLOG->info("current setting.ini is latest {} {}", iniVersion.toStdString(), iniFile.toStdString());
        }
    }
    else if (GLOG)
    {
        GLOG->info("no setting.ini on udisk");
    }

    // 应用程序更新，海图也可以打包在这个zip里一起更新
    filter.clear();
    files.clear();
    filter << "blackbox????????.zip";
    files = dir.entryList(filter, QDir::Files | QDir::Readable, QDir::Name);
    if (files.size() == 0)
        return false;
    m_fileName = files.last();

    QString currentVersion = setting.getSetting(SETTING_APPVERSION);
    if (m_fileName > currentVersion)
        return true;
    m_fileName.clear();
    return false;
#else
    return false;
#endif
}

bool UsbUpdate::update()
{
    if (m_fileName.isEmpty())
        return false;
    QTextCodec *fileNameCodec = QTextCodec::codecForName("GBK");
    QString fullPath = m_appDir + "/";

#if defined(Q_OS_LINUX)
    fullPath += "../";
#endif
    QStringList list = YCompress::extractDirWithRename("/udisk/" + m_fileName, fileNameCodec, fullPath);
    if (list.count() > 0)
    {
        DbSetting setting;
        setting.saveSetting(SETTING_APPVERSION, m_fileName);
        return true;
    }
    return false;
}
